<?php
namespace app\controller;

use app\BaseController;
use app\model\ClassGroupPhoto;
use app\model\ClassInfo;
use app\model\ClassNotice;
use app\model\User;
use app\Request;
class ClassInfoController extends BaseController
{
    /**
     * 获取班级信息
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getClassInfo(Request $request){
        //获取用户id
        $uid = $request->user_id;
        //通过获取到的用户id去查找到class_id，并且将它写成一个叫class_id的变量($class_id
        $class_id = User::where('id','=',$uid)->value('class_id');
        //先看下面一行代码 得知我们需要查找很多个字段 所有将他们封装成一个数组变量
        $findInfo = ['class_name','class_training','entrance_time','graduation_time','headmaster','headmaster_phone'];
        //运用上面的变量去查找出需要的班级信息
        $class_info = ClassInfo::Where('id','=',$class_id)->field($findInfo)->select();
        return $this->returnJson(self::SUCCESS,'成功',$class_info);
    }

    /**
     * 获取班级照片
     * @param Request $request
     * @return mixed]
     */
    public function getClassPhoto(Request $request){
        $uid = $request->user_id;
        $class_id = User::where('id','=',$uid)->value('class_id');
        $class_photo = ClassGroupPhoto::Where('id','=',$class_id)->value('class_photo');
        return $this->returnJson(self::SUCCESS,'成功',$class_photo);
    }

    /**
     * 修改班级信息
     * @param Request $request
     * @return mixed
     */
    public function putClassInfo(Request $request){
        $uid = $request->user_id;
        $data = $request->param(['class_name','class_training','entrance_time','graduation_time','headmaster','headmaster_phone']);
        $isUpdate = ClassInfo::where('id','=',$uid)->update([
            'class_name'        =>  $data['class_name'],
            'class_training'    =>  $data['class_training'],
            'entrance_time'     =>  $data['entrance_time'],
            'graduation_time'   =>  $data['graduation_time'],
            'headmaster'        =>  $data['headmaster'],
            'headmaster_phone'  =>  $data['headmaster_phone']
        ]);
        if ($isUpdate != 1){
            return $this->returnJson(self::SUCCESS,'失败','');
        }
        return $this->returnJson(self::SUCCESS,'成功','');
    }

    /**
     * 修改班级照片
     * @param Request $request
     * @return mixed
     */
    public function putClassPhoto(Request $request){
        $uid = $request->user_id;
        $data = $request->param(['class_photo']);
        $isUpdate = ClassInfo::where('id','=',$uid)->update([
            'class_photo'        =>  $data['class_photo']
        ]);
        if ($isUpdate != 1){
            return $this->returnJson(self::SUCCESS,'失败','');
        }
        return $this->returnJson(self::SUCCESS,'成功','');
    }

    /**
     * 获取公告
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNotice(){
        $data = ClassNotice::field(['id','notice_name','notice_content','urgency','end_time'])->order('urgency asc')->select();
        return $this->returnJson(self::SUCCESS,'获取成功',$data);
    }

    /**
     * 删除公告
     * @param Request $request
     * @return mixed
     */
    public function deleteNotice(Request $request){
        $nid = $request->delete('notice_id');
        $isDelete = ClassNotice::where('id','=',$nid)->useSoftDelete('delete_time',time())->delete();
        if ($isDelete != 1){
            return $this->returnJson(self::SERVER_ERROR,'删除失败','');
        }
        return $this->returnJson(self::SUCCESS,'删除成功','');
    }

    /**
     * 新增公告
     * @param Request $request
     * @return mixed
     */
    public function addNotice(Request $request){
        $getData = $request->param(['title','content','urgency','end_time']);
        $isSuccess = ClassNotice::create([
            'notice_name'       => $getData['title'],
            'notice_content'    => $getData['content'],
            'urgency'           => $getData['urgency'],
            'end_time'          => $getData['end_time'],
            'create_time'       => time()
        ]);
        if (empty($isSuccess)){
            return $this->returnJson(self::SERVER_ERROR,'新增失败','');
        }
        return $this->returnJson(self::SUCCESS,'新增成功','');
    }
}